﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;

public partial class Store_Manager_SM_Subcategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["current"] = "Product Sub Category";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }
    }

    #region Validation
    protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        try
        {
            // Displays the error row if there is at least one error.
            if (e.Errors.Count > 0) e.RowError = "Please, fill all fields.";

            if (e.NewValues["Cat_Name"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Cat_Name"],
                "Please Select Category Name");
            }
            if (e.NewValues["Sub_Cat"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Sub_Cat"],
                "Please Enter Subcategory ID");
            }
            if (e.NewValues["Description"] != null && e.NewValues["Description"].ToString().Length < 2)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Description"],
                "Description must be at least two characters long.");
            }
            if (e.NewValues["Sub_Cat_Name"] == null || e.NewValues["Sub_Cat_Name"].ToString().Length < 2)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Sub_Cat_Name"],
                "Subcategory Name must be at least two characters long.");
            }
            if (e.NewValues["Status"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Status"],
                "Please Select Status");
            }

            if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0)
                e.RowError = "You must enter a value for all required fields";
        }
        catch
        {
            e.RowError = "Please Check The Data You Entered are in Correct Format";
        }
    }

    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
    {
        try
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }

    protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
    {
        try
        {
            // Validates the edited row if it isn't a new row,.
            if (!ASPxGridView1.IsNewRowEditing)
                ASPxGridView1.DoRowValidation();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }
    #endregion

}